/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.biz.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.action.AbstractAction;
import com.mttang.framework.core.exception.MtTangServiceRuntimeException;
import com.mttang.framework.core.paginated.util.PagedFliper;
import com.mttang.framework.core.pojos.ServiceRequest;
import com.mttang.framework.core.pojos.ServiceResponse;
import com.mttang.info.core.ParameterModel;
import com.mttang.info.core.ResultDTO;
import com.mttang.info.core.ResultDTOWrapper;
import com.mttang.info.core.ServiceRequestAnalyser;
import com.mttang.sys.biz.service.MenuService;
import com.mttang.sys.biz.service.RoleService;
import com.mttang.sys.biz.service.UserService;
import com.mttang.sys.dto.MenuDTO;
import com.mttang.sys.dto.RoleDTO;
import com.mttang.sys.dto.RolePrivilegeDTO;
import com.mttang.sys.dto.UserDTO;
import com.mttang.util.InfoAppKeyConstant;
import com.mttang.util.SysBeanConstant;

/**
 * 类RoleAction.java的实现描述：处理角色TODO 类实现描述
 * 
 * @author acc 2010-6-1 下午07:57:07
 */
public class RoleAction extends AbstractAction {

	private Log logger = LogFactory.getLog(getClass());

	private RoleService service;

	public void setService(RoleService service) {
		this.service = service;
	}

	public ServiceResponse perform(ServiceRequest request)
			throws MtTangServiceRuntimeException {
		return null;
	}

	@SuppressWarnings("unchecked")
	public Map<Object, Object> query(Map<String, Object> map2) {
		PagedFliper pagedFliper = (PagedFliper) map2
				.get(InfoAppKeyConstant.K_pageModel);
		Map<String, Object> pMap = (Map<String, Object>) map2
				.get(InfoAppKeyConstant.K_parameter);
		ParameterModel parameterModel = new ParameterModel();
		parameterModel.setPagedFliper(pagedFliper);
		parameterModel.setPramater(pMap);
		ResultDTO<RoleDTO> list = service.findRole(parameterModel);
		ResultDTOWrapper wrapper = new ResultDTOWrapper(list);
		wrapper.setPagedFliper(pagedFliper); // 重新设置分页信息
		Map<Object, Object> map = wrapper.wrap();
		return map;
	}

	@SuppressWarnings("unchecked")
	public Map<Object, Object> add(Map<String, Object> map2) {
		String roleName = String.valueOf(map2.get("roleName"));
		RoleDTO rdto = service.findByName(roleName);
		Map<Object, Object> param = new HashMap<Object, Object>();
		if (rdto != null) {
			param.put("count", rdto.getRoleId());
			return param;
		}
		String[] linkId = (String[]) map2.get("linkId");
		RoleDTO dto = new RoleDTO();
		dto.setRoleName(roleName);
		dto.setLinkId(linkId);
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		service.insert(dto, logMap);
		return null;
	}

	@SuppressWarnings("unchecked")
	public Map<Object, Object> update(Map<String, Object> map2) {

		// 修改
		String roleName = String.valueOf(map2.get("roleName"));
		String roleName1 = String.valueOf(map2.get("roleName1"));
		if (!roleName1.equals(roleName)) {
			RoleDTO rdto = service.findByName(roleName);
			Map<Object, Object> param = new HashMap<Object, Object>();
			if (rdto != null) {
				param.put("count", rdto.getRoleId());
				return param;
			}
		}
		String[] linkId = (String[]) map2.get("linkId");
		Integer roleId = (Integer) map2.get("roleId");
		RoleDTO dto = new RoleDTO();
		dto.setRoleName(roleName);
		dto.setRoleId(roleId);
		dto.setLinkId(linkId);
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		service.update(dto, logMap);
		return null;

	}
	
	@SuppressWarnings("unchecked")
	public Map<Object,Object> delete (Map<String,Object> map2){
		// 删除
		Integer roleId = (Integer) map2.get("roleId");
		UserService userService = (UserService) context
				.getBean(SysBeanConstant.SERVICE_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		RoleDTO rdto = new RoleDTO();
		rdto.setRoleId(roleId);
		map.put("roleId", roleId.toString());
		Map<Object, Object> param = new HashMap<Object, Object>();
		if (userService.findCount(map) == 0) {
			Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
			service.delete(rdto, logMap);
		} else {
			param.put("error", "error");
		}
		return param;
	}
	
    public Map<Object,Object> searchById(Map<String,Object> map2){
		// 根据ID查询
		Integer roleId = (Integer) map2.get("roleId");
		MenuService menuService = (MenuService) context
				.getBean(SysBeanConstant.SERVICE_MENU);
		// List<MenuDTO> list = menuService.findMenuTree(roleId);
		List<MenuDTO> list = new ArrayList<MenuDTO>();
		Map<String, Object> onelist = new HashMap<String, Object>();
		onelist.put("menuLevel", 1);
		onelist.put("menuPid", -1);
		List<MenuDTO> listone = menuService.findMenuTree(onelist);
		if (listone != null && listone.size() > 0)
			for (int i = 0; i < listone.size(); i++) {
				Map<String, Object> twolist = new HashMap<String, Object>();
				list.add(listone.get(i));
				twolist.put("menuLevel", 2);
				twolist.put("menuPid", listone.get(i).getMenuId());
				List<MenuDTO> listtwo = menuService.findMenuTree(twolist);
				if (listtwo != null && listtwo.size() > 0) {
					for (int j = 0; j < listtwo.size(); j++) {
						list.add(listtwo.get(j));
						Map<String, Object> threelist = new HashMap<String, Object>();
						threelist.put("menuLevel", 3);
						threelist
								.put("menuPid", listtwo.get(j).getMenuId());
						List<MenuDTO> listthree = menuService
								.findMenuTree(threelist);
						if (listthree != null && listthree.size() > 0) {
							for (int k = 0; k < listthree.size(); k++) {
								list.add(listthree.get(k));
							}

						}
					}
				}
			}

		List<RolePrivilegeDTO> linklist = service.findRoelPrv(roleId);
		List<RolePrivilegeDTO> llist = new ArrayList<RolePrivilegeDTO>();
		Map<Object, Object> linkmap = new HashMap<Object, Object>();
		List<RolePrivilegeDTO> link = new ArrayList<RolePrivilegeDTO>();
		int menuId = 0;
		for (int i = 0; i < linklist.size(); i++) {
			RolePrivilegeDTO dto = linklist.get(i);
			RolePrivilegeDTO rp = new RolePrivilegeDTO();
			if (dto.getLinkId() != menuId) {
				if (menuId != 0) {
					rp.setMenuId(linklist.get(i - 1).getMenuId());
					rp.setList(llist);
					link.add(rp);
					llist = new ArrayList<RolePrivilegeDTO>();
				}
				llist.add(dto);
				menuId = dto.getLinkId();
			} else {
				llist.add(dto);
			}
		}
		RolePrivilegeDTO rp = new RolePrivilegeDTO();
		rp.setList(llist);
		rp.setMenuId(linklist.get(linklist.size() - 1).getMenuId());
		link.add(rp);
		linkmap.put("list", link);
		RoleDTO dto = service.findById(roleId);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("mlist", list);
		map.put("linklist", linkmap);
		map.put("role", dto);
		return map;

	}
    
    public Map<Object,Object> roleAdd(Map<String,Object> map2){
		MenuService menuService = (MenuService) context
				.getBean(SysBeanConstant.SERVICE_MENU);
		List<MenuDTO> list = new ArrayList<MenuDTO>();
		Map<String, Object> onelist = new HashMap<String, Object>();
		onelist.put("menuLevel", 1);
		onelist.put("menuPid", -1);
		List<MenuDTO> listone = menuService.findMenuTree(onelist);
		if (listone != null && listone.size() > 0)
			for (int i = 0; i < listone.size(); i++) {
				list.add(listone.get(i));
				Map<String, Object> twolist = new HashMap<String, Object>();
				twolist.put("menuLevel", 2);
				twolist.put("menuPid", listone.get(i).getMenuId());
				List<MenuDTO> listtwo = menuService.findMenuTree(twolist);
				if (listtwo != null && listtwo.size() > 0) {
					for (int j = 0; j < listtwo.size(); j++) {
						list.add(listtwo.get(j));
						Map<String, Object> threelist = new HashMap<String, Object>();
						threelist.put("menuLevel", 3);
						threelist
								.put("menuPid", listtwo.get(j).getMenuId());
						List<MenuDTO> listthree = menuService
								.findMenuTree(threelist);
						if (listthree != null && listthree.size() > 0) {
							for (int k = 0; k < listthree.size(); k++) {
								list.add(listthree.get(k));
							}

						}
					}
				}
			}
		List<RolePrivilegeDTO> linklist = service.findRoelPrv(-1);
		List<RolePrivilegeDTO> llist = new ArrayList<RolePrivilegeDTO>();
		Map<Object, Object> linkmap = new HashMap<Object, Object>();
		List<RolePrivilegeDTO> link = new ArrayList<RolePrivilegeDTO>();
		int menuId = 0;
		for (int i = 0; i < linklist.size(); i++) {
			RolePrivilegeDTO dto = linklist.get(i);
			RolePrivilegeDTO rp = new RolePrivilegeDTO();
			if (dto.getLinkId() != menuId) {
				if (menuId != 0) {
					rp.setMenuId(linklist.get(i - 1).getMenuId());
					rp.setList(llist);
					link.add(rp);
					llist = new ArrayList<RolePrivilegeDTO>();
				}
				llist.add(dto);
				menuId = dto.getLinkId();
			} else {
				llist.add(dto);
			}
		}
		RolePrivilegeDTO rp = new RolePrivilegeDTO();
		rp.setList(llist);
		rp.setMenuId(linklist.get(linklist.size() - 1).getMenuId());
		link.add(rp);
		linkmap.put("list", link);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("mlist", list);
		map.put("linklist", linkmap);
		return map;
    }
    
    public Map<Object,Object> role(Map<String,Object> map2){
		String roleName = String.valueOf(map2.get("roleName"));
		RoleDTO dto = service.findByName(roleName);
		Map<Object, Object> param = new HashMap<Object, Object>();
		if (dto != null) {
			param.put("count", dto.getRoleId());
		}
		return param;
	}
    
    @SuppressWarnings("unchecked")
	public Map<Object,Object> roleUser(Map<String,Object> map2){
		// 人员表
		UserService userService = (UserService) context
				.getBean(SysBeanConstant.SERVICE_USER);
		ParameterModel parameterModel = new ParameterModel(); 
		PagedFliper pagedFliper = (PagedFliper) map2.get(InfoAppKeyConstant.K_pageModel);  
		Map<String, Object> pmap  = (Map<String, Object>) map2.get(InfoAppKeyConstant.K_parameter);
		parameterModel.setPagedFliper(pagedFliper);
		parameterModel.setPramater(pmap);
		ResultDTO<UserDTO> list = userService.findUser(parameterModel);
		ResultDTOWrapper wrapper = new ResultDTOWrapper(list);
		wrapper.setPagedFliper(pagedFliper); // 重新设置分页信息
		Map<Object, Object> map = wrapper.wrap();
		return map;
	} 
    
    public Map<Object,Object> prv(Map<String,Object> map2){

        //该角色拥有该菜单的具体权限
         
          String roleId = String.valueOf(map2.get("roleId"));
          String menuId = String.valueOf(map2.get("menuId"));
          Map<Object, Object> param = new HashMap<Object, Object>();
          param.put("roleId", roleId);
          param.put("menuId", menuId);
          Map<Object, Object> map = new HashMap<Object, Object>();
          Map<Object, Object> maplist = new HashMap<Object, Object>();
          List<RolePrivilegeDTO> list = service.findPrv(param);
          for (int i = 0; i < list.size(); i++) {
              map.put(list.get(i).getValue(), "true");
          }
          maplist.put("prv", map);
          return maplist;
      
    }
    
}
